﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Shop.Entity;
using Shop.Tools;

namespace Shop.Web.Filter
{
    public class ExceptionFilter : IExceptionFilter
    {
        public void OnException(ExceptionContext context)
        {
            StaticConst.log.Error("系统全局错误", context.Exception);
            //指定返回 返回格式
            if (context.HttpContext.Request.Headers.ContainsKey("x-requested-with") && context.HttpContext.Request.Headers["x-requested-with"] == "XMLHttpRequest")
            {
                context.Result = new JsonResult(new ResultModel { code = OperationResultType.Error, data = "", error_msg = "系统异常", success = false });
            }
            else
            {
                context.Result = new BadRequestObjectResult("系统异常，请刷新再试");
            }
        }
    }
}
